<template>
	<uni-nav-bar title="配送费膨胀-人车验证" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package_sale" style="color: #558AF1;">车牌号匹配成功，请上传您与本单实际加注车辆合影</view>
	<view class="package_sale">
		<view>
			<view>请上传合影，保证人与车牌号清晰</view>
			<mk-upload :imgList="imgList" @onDelete="onDelete" @onChoose="onChoose" :maxCount="1"
				:sourceType="sourceType" />
		</view>
		<view class="but" @click="submit()">完成膨胀</view>
	</view>
	<Watermark ref="watermark" @selected="selected"></Watermark>
	<!-- 达到上限提示 -->
	<uni-popup ref="upper" type="center" style="z-index: 910" :is-mask-click="false">
		<view class="package" style="width:650rpx;padding:30rpx;">
			<view>您已获得车友{{name}}{{money}}元的配送费膨胀</view>
			<view class="space-between buts">
				<view @click="upperClose()">去查看</view>
				<view @click="upperClose()">我知道了</view>
			</view>
		</view>
	</uni-popup>
</template>

<script setup lang="ts">
	import { navBack, navTo, redirectTo } from '@/utils/navigator';
	import { errorToast, showLoading, successToast } from '@/utils/prompt';
	import { ref } from 'vue';
	import { getStaffDriverOrderInfo } from '@/gql/earthPushing/teamJoin';
	import { onLoad } from '@dcloudio/uni-app';
	import Watermark from '@c/watermark.vue';
	import { toPublish } from '@mqtt';
	import { getDriverID } from '@/stores/driverID';
	const staff_id = getDriverID();
	const upper = ref(null)
	const numberplate = ref(''); //车牌号
	const list = ref([])
	const money = ref()
	const sourceType = ["camera"]
	const orderLists = ref()
	// 上传图片
	const imgList = ref([])
	const image = ref()
	const watermark = ref(null)
	const name = ref('')
	const orderID = ref()
	onLoad((pearm) => {
		name.value = pearm.name
		orderID.value = Number(pearm.id)
		init()
	})
	function init() {
		showLoading()
		const payload = {
			query: getStaffDriverOrderInfo,
			variables: {
				id: orderID.value
			},
		};
		toPublish(
			'ql/staff/getStaffDriverOrderInfo',
			payload,
			(obj : any) => {
				const { getStaffDriverOrderInfo } = obj.data;
				orderLists.value = getStaffDriverOrderInfo
				if (getStaffDriverOrderInfo.staff_image) {
					numberplate.value = getStaffDriverOrderInfo.check_cart_code ? getStaffDriverOrderInfo.check_cart_code : '';
					list.value = numberplate.value ? numberplate.value.split('') : []
					imgList.value.push(getStaffDriverOrderInfo.staff_image)
					image.value = '/uploads'+getStaffDriverOrderInfo.staff_image.split('/uploads')[1]
				}
			}
		);
	}
	// 上传图片
	function onChoose(e : any) {
		watermark.value.fillTextToImg(e.tempFilePaths[0])
		console.log(e.tempFilePaths[0])
	}
	function selected(e : any) {
		uni.uploadFile({
			url: 'https://admin.anlan.tech/api/common/upload3',
			filePath: e,
			name: 'file',
			formData: {
				'user': 'test'  // 上传附带参数
			},
			success: (uploadFileRes) => {
				// 根据接口具体返回格式   赋值具体对应url
				console.log(JSON.stringify(uploadFileRes.data))
				const state = JSON.parse(uploadFileRes.data).data
				numberplate.value = state.cart_code ? state.cart_code : '';
				list.value = numberplate.value ? numberplate.value.split('') : []
				imgList.value.push(state.fullurl)
				image.value = state.url
			},
			fail: (err : any) => {
				console.log('err', err)
			}
		});
	}
	// 删除图片
	function onDelete() {
		imgList.value = []
		image.value = ''
		numberplate.value = ''
		list.value = []
	}
	//提交
	function submit() {
		if (!image.value) {
			errorToast('请上传图片')
			return
		}
		if (!numberplate.value) {
			errorToast('请上传含有车牌照的图片')
			return
		}
		showLoading()
		const payload = {
			staff_id,
			cart_code: numberplate.value,
			image: image.value,
			id: orderID.value,
		};
		toPublish('staff/addStaffCartImage',
			payload,
			(obj : any) => {
				uni.hideLoading();
				const { code, msg, data } = obj;
				if (code === 1) {
					money.value = data.multiple_fee
					successToast(msg)
					setTimeout(() => {
						upperOpen()
					}, 1000)
				} else {
					errorToast(msg)
				}
			}
		);
	}

	function upperOpen() {
		upper.value.open()
	}
	function upperClose() {
		redirectTo('Sale/delivery/deliveryList')
		upper.value.close()
	}
</script>

<style scoped lang="less">
	.but {
		width: 360rpx;
		height: 98rpx;
		background: #558AF1;
		border-radius: 5px;
		color: #fff;
		text-align: center;
		line-height: 98rpx;
		margin: 80rpx auto 30rpx;
	}

	.buts {
		width: 100%;
		justify-content: space-around;
		margin: 60rpx 0px 20rpx;

		view {
			width: 230rpx;
			height: 69rpx;
			border-radius: 5px;
			text-align: center;
			line-height: 69rpx;
		}

		view:nth-of-type(1) {
			color: #468FF5;
			border: 1px solid #468FF5;
			box-sizing: border-box;
		}

		view:nth-of-type(2) {
			background: #468FF5;
			color: #FFF;
		}
	}
</style>
<style>
	.package_sale>>>.mk-upload {
		padding: 0px;
	}
</style>